package com.example.esdemo.service.impl;

import com.example.esdemo.entity.EsTestBean;
import com.example.esdemo.entity.PageEntity;
import com.example.esdemo.service.IEsTestService;
import com.google.gson.Gson;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: kaibo
 * @date: 2020/11/24 18:54
 * @GitHub: https://github.com/yuxuelian
 * @qq: 568966289
 * @description:
 */


@Service
public class EsTestServiceImpl implements IEsTestService {

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    Gson gson;

    @Override
    public EsTestBean save(EsTestBean esTestBean) {
        esTestBean.setId("");
        IndexRequest request = new IndexRequest(EsTestBean.INDEX_NAME, EsTestBean.TYPE)
                .source(gson.toJson(esTestBean), XContentType.JSON);
        try {
            IndexResponse response = elasticsearchRestTemplate.getClient().index(request, RequestOptions.DEFAULT);
            if (response.getResult().getOp() == DocWriteResponse.Result.CREATED.getOp()) {
                esTestBean.setId(response.getId());
                return esTestBean;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public EsTestBean findById(String id) {
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.idsQuery(EsTestBean.TYPE).addIds(id))
                .withIndices(EsTestBean.INDEX_NAME)
                .withTypes(EsTestBean.TYPE)
                .build();
        List<EsTestBean> esTestBeans = elasticsearchRestTemplate.queryForList(searchQuery, EsTestBean.class);
        if (esTestBeans.size() > 0) {
            return esTestBeans.get(0);
        }
        return null;
    }


    @Override
    public List<EsTestBean> findAll() {
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withIndices(EsTestBean.INDEX_NAME)
                .withTypes(EsTestBean.TYPE)
                .build();
        return elasticsearchRestTemplate.queryForList(searchQuery, EsTestBean.class);
    }


    @Override
    public PageEntity<EsTestBean> pageQuery(int page, int pageSize) {
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withIndices(EsTestBean.INDEX_NAME)
                .withTypes(EsTestBean.TYPE)
                .withPageable(PageRequest.of(page, pageSize))
                .build();
        AggregatedPage<EsTestBean> aggregatedPage = elasticsearchRestTemplate.queryForPage(searchQuery, EsTestBean.class);
        return PageEntity.map(aggregatedPage);
    }
}
